<!DOCTYPE html>
<html lang=zh>
<head>
    <!-- so meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="HandheldFriendly" content="True">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
    <meta name="description" content="实现定时任务的几种方式 Timer: 这是java自带的java.util.Timer类，这个类运行你调度一个java.util.TimerTask任务。使用这种方式可以让程序按照某一个频度执行，但不能在指定时间运行。一般用的较少。多线程并行处理定时任务，Timer运行多个TimeTask时，只要其中之一没有捕获抛出的异常，其它任务便会自动终止运行  ScheduledExecutorServic">
<meta property="og:type" content="article">
<meta property="og:title" content="Spring Boot实现定时任务的4种方式">
<meta property="og:url" content="https:&#x2F;&#x2F;fengzxia.gitee.io&#x2F;posts&#x2F;493442a1.html">
<meta property="og:site_name" content="只是学习学习的博客">
<meta property="og:description" content="实现定时任务的几种方式 Timer: 这是java自带的java.util.Timer类，这个类运行你调度一个java.util.TimerTask任务。使用这种方式可以让程序按照某一个频度执行，但不能在指定时间运行。一般用的较少。多线程并行处理定时任务，Timer运行多个TimeTask时，只要其中之一没有捕获抛出的异常，其它任务便会自动终止运行  ScheduledExecutorServic">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2019-04-04T07:24:16.000Z">
<meta property="article:modified_time" content="2020-04-17T10:28:04.381Z">
<meta property="article:author" content="只是学习学习">
<meta property="article:tag" content="定时任务">
<meta name="twitter:card" content="summary">
    
    
        
          
              <link rel="shortcut icon" href="/images/favicon.ico">
          
        
        
          
            <link rel="icon" type="image/png" href="/images/favicon-192x192.png" sizes="192x192">
          
        
        
          
            <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon.png">
          
        
    
    <!-- title -->
    <title>Spring Boot实现定时任务的4种方式</title>
    <!-- styles -->
    
<link rel="stylesheet" href="/css/style.css">

    <!-- persian styles -->
    
      
<link rel="stylesheet" href="/css/rtl.css">

    
    <!-- rss -->
    
    
    
<meta name="generator" content="Hexo 4.1.1"></head>

<body class="max-width mx-auto px3 ltr">
    
      <div id="header-post">
  <a id="menu-icon" href="#"><i class="fas fa-bars fa-lg"></i></a>
  <a id="menu-icon-tablet" href="#"><i class="fas fa-bars fa-lg"></i></a>
  <a id="top-icon-tablet" href="#" onclick="$('html, body').animate({ scrollTop: 0 }, 'fast');" style="display:none;"><i class="fas fa-chevron-up fa-lg"></i></a>
  <span id="menu">
    <span id="nav">
      <ul>
         
          <li><a href="/">主页</a></li>
         
          <li><a href="/archives/">归档</a></li>
         
          <li><a href="/categories/">分类</a></li>
         
          <li><a href="/tags/">标签</a></li>
         
          <li><a href="/search/">搜索</a></li>
         
          <li><a href="/about/">关于</a></li>
         
          <li><a href="/friend_ship/">友链</a></li>
        
      </ul>
    </span>
    <br/>
    <span id="actions">
      <ul>
        
        <li><a class="icon" href="/posts/9c0cd83e.html"><i class="fas fa-chevron-left" aria-hidden="true" onmouseover="$('#i-prev').toggle();" onmouseout="$('#i-prev').toggle();"></i></a></li>
        
        
        <li><a class="icon" href="/posts/1a2c31cb.html"><i class="fas fa-chevron-right" aria-hidden="true" onmouseover="$('#i-next').toggle();" onmouseout="$('#i-next').toggle();"></i></a></li>
        
        <li><a class="icon" href="#" onclick="$('html, body').animate({ scrollTop: 0 }, 'fast');"><i class="fas fa-chevron-up" aria-hidden="true" onmouseover="$('#i-top').toggle();" onmouseout="$('#i-top').toggle();"></i></a></li>
        <li><a class="icon" href="#"><i class="fas fa-share-alt" aria-hidden="true" onmouseover="$('#i-share').toggle();" onmouseout="$('#i-share').toggle();" onclick="$('#share').toggle();return false;"></i></a></li>
      </ul>
      <span id="i-prev" class="info" style="display:none;">上一篇</span>
      <span id="i-next" class="info" style="display:none;">下一篇</span>
      <span id="i-top" class="info" style="display:none;">返回顶部</span>
      <span id="i-share" class="info" style="display:none;">分享文章</span>
    </span>
    <br/>
    <div id="share" style="display: none">
      <ul>
  <li><a class="icon" href="http://www.facebook.com/sharer.php?u=https://fengzxia.gitee.io/posts/493442a1.html" target="_blank" rel="noopener"><i class="fab fa-facebook " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://twitter.com/share?url=https://fengzxia.gitee.io/posts/493442a1.html&text=Spring Boot实现定时任务的4种方式" target="_blank" rel="noopener"><i class="fab fa-twitter " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://www.linkedin.com/shareArticle?url=https://fengzxia.gitee.io/posts/493442a1.html&title=Spring Boot实现定时任务的4种方式" target="_blank" rel="noopener"><i class="fab fa-linkedin " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://pinterest.com/pin/create/bookmarklet/?url=https://fengzxia.gitee.io/posts/493442a1.html&is_video=false&description=Spring Boot实现定时任务的4种方式" target="_blank" rel="noopener"><i class="fab fa-pinterest " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="mailto:?subject=Spring Boot实现定时任务的4种方式&body=Check out this article: https://fengzxia.gitee.io/posts/493442a1.html"><i class="fas fa-envelope " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://getpocket.com/save?url=https://fengzxia.gitee.io/posts/493442a1.html&title=Spring Boot实现定时任务的4种方式" target="_blank" rel="noopener"><i class="fab fa-get-pocket " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://reddit.com/submit?url=https://fengzxia.gitee.io/posts/493442a1.html&title=Spring Boot实现定时任务的4种方式" target="_blank" rel="noopener"><i class="fab fa-reddit " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://www.stumbleupon.com/submit?url=https://fengzxia.gitee.io/posts/493442a1.html&title=Spring Boot实现定时任务的4种方式" target="_blank" rel="noopener"><i class="fab fa-stumbleupon " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://digg.com/submit?url=https://fengzxia.gitee.io/posts/493442a1.html&title=Spring Boot实现定时任务的4种方式" target="_blank" rel="noopener"><i class="fab fa-digg " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://www.tumblr.com/share/link?url=https://fengzxia.gitee.io/posts/493442a1.html&name=Spring Boot实现定时任务的4种方式&description=" target="_blank" rel="noopener"><i class="fab fa-tumblr " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://news.ycombinator.com/submitlink?u=https://fengzxia.gitee.io/posts/493442a1.html&t=Spring Boot实现定时任务的4种方式" target="_blank" rel="noopener"><i class="fab fa-hacker-news " aria-hidden="true"></i></a></li>
</ul>

    </div>
    <div id="toc">
      <ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#实现定时任务的几种方式"><span class="toc-number">1.</span> <span class="toc-text">实现定时任务的几种方式</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#使用Timer"><span class="toc-number">1.1.</span> <span class="toc-text">使用Timer</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#使用ScheduledExecutorService"><span class="toc-number">1.2.</span> <span class="toc-text">使用ScheduledExecutorService</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#使用Spring-Task"><span class="toc-number">1.3.</span> <span class="toc-text">使用Spring Task</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#修改Spring-Task为多线程执行"><span class="toc-number">1.3.1.</span> <span class="toc-text">修改Spring Task为多线程执行</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#执行时间的配置"><span class="toc-number">1.3.2.</span> <span class="toc-text">执行时间的配置</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#cron表达式详细参考下边的博客"><span class="toc-number">1.3.3.</span> <span class="toc-text">cron表达式详细参考下边的博客</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#整合Quartz"><span class="toc-number">1.4.</span> <span class="toc-text">整合Quartz</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#1-创建任务类"><span class="toc-number">1.4.1.</span> <span class="toc-text">1.创建任务类</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#2-创建配置类QuartzConfig"><span class="toc-number">1.4.2.</span> <span class="toc-text">2.创建配置类QuartzConfig</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#3-启动项目"><span class="toc-number">1.4.3.</span> <span class="toc-text">3.启动项目</span></a></li></ol></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Spring-Task和Quarz的区别"><span class="toc-number">2.</span> <span class="toc-text">Spring Task和Quarz的区别</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Demo地址"><span class="toc-number">3.</span> <span class="toc-text">Demo地址</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#参考博客地址"><span class="toc-number">4.</span> <span class="toc-text">参考博客地址</span></a></li></ol>
    </div>
  </span>
</div>

    
    <div class="content index py4">
        
        <article class="post" itemscope itemtype="http://schema.org/BlogPosting">
  <header>
    
    <h1 class="posttitle" itemprop="name headline">
        Spring Boot实现定时任务的4种方式
    </h1>



    <div class="meta">
      <span class="author" itemprop="author" itemscope itemtype="http://schema.org/Person">
        <span itemprop="name">只是学习学习的博客</span>
      </span>
      
    <div class="postdate">
      
        <time datetime="2019-04-04T07:24:16.000Z" itemprop="datePublished">2019-04-04</time>
        
      
    </div>


      
    <div class="article-category">
        <i class="fas fa-archive"></i>
        <a class="category-link" href="/categories/Java/">Java</a> › <a class="category-link" href="/categories/Java/Spring/">Spring</a> › <a class="category-link" href="/categories/Java/Spring/SpringBoot/">SpringBoot</a> › <a class="category-link" href="/categories/%E8%BD%AC%E8%BD%BD/">转载</a>
    </div>


      
    <div class="article-tag">
        <i class="fas fa-tag"></i>
        <a class="tag-link" href="/tags/%E5%AE%9A%E6%97%B6%E4%BB%BB%E5%8A%A1/" rel="tag">定时任务</a>
    </div>


    </div>
  </header>
  

  <div class="content"itemprop ="articleBody">
    <h2 id="实现定时任务的几种方式"><a href="#实现定时任务的几种方式" class="headerlink" title="实现定时任务的几种方式"></a>实现定时任务的几种方式</h2><ul>
<li><p>Timer: 这是java自带的java.util.Timer类，这个类运行你调度一个java.util.TimerTask任务。使用这种方式可以让程序按照某一个频度执行，但不能在指定时间运行。一般用的较少。<strong>多线程并行处理定时任务，Timer运行多个TimeTask时，只要其中之一没有捕获抛出的异常，其它任务便会自动终止运行</strong></p>
</li>
<li><p>ScheduledExecutorService: 也是jdk自带的一个类；是基于线程池设计的定时任务类，每个调度任务都会分配到线程池中的一个线程去执行，<strong>任务并发执行，互不影响</strong></p>
</li>
<li><p>Spring Task: Spring3.0以后自带的task，可以将它看成一个轻量级的Quartz，而且使用起来比Quartz简单许多<strong>所有的定时任务都在同一线程上串行，想要异步执行需要自己添加线程池</strong></p>
</li>
<li><p>Quartz: 这是一个功能比较强大的调度器，可以让你的程序在指定时间执行，也可以按照某一个频度执行，配置起来稍显复杂</p>
<h3 id="使用Timer"><a href="#使用Timer" class="headerlink" title="使用Timer"></a>使用Timer</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">TestTimer</span> </span>&#123;</span><br><span class="line">  <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">    TimerTask timerTask = <span class="keyword">new</span> TimerTask() &#123;</span><br><span class="line">      <span class="meta">@Override</span></span><br><span class="line">      <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">run</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        System.out.println(<span class="string">"task run:"</span> + System.currentTimeMillis());</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;;</span><br><span class="line">    <span class="comment">//多线程并行处理定时任务时，Timer运行多个TimeTask时，只要其中之一没有捕获抛出的异常，其它任务便会自动终止运行</span></span><br><span class="line">    <span class="comment">//并不推荐使用</span></span><br><span class="line">    Timer timer = <span class="keyword">new</span> Timer();</span><br><span class="line">    <span class="comment">//安排指定的任务在指定的时间开始进行重复的固定延迟执行，这里是延时10毫秒每3秒执行一次</span></span><br><span class="line">    timer.schedule(timerTask, <span class="number">10</span>, <span class="number">3000</span>);</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

</li>
</ul>
<h3 id="使用ScheduledExecutorService"><a href="#使用ScheduledExecutorService" class="headerlink" title="使用ScheduledExecutorService"></a>使用ScheduledExecutorService</h3><p>该方法跟Timer类似</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">TestScheduledExecutorService</span> </span>&#123;</span><br><span class="line">  <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">    ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();</span><br><span class="line">    <span class="comment">// 参数：1.任务体，2.首次执行的延时时间</span></span><br><span class="line">    <span class="comment">//      3.任务执行间隔，4.间隔时间单位</span></span><br><span class="line">    service.scheduleAtFixedRate(<span class="keyword">new</span> Runnable() &#123;</span><br><span class="line">      <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">run</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        System.out.println(<span class="string">"task ScheduledExecutorService:"</span> + System.currentTimeMillis());</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;, <span class="number">0</span>, <span class="number">3</span>, TimeUnit.SECONDS);</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="使用Spring-Task"><a href="#使用Spring-Task" class="headerlink" title="使用Spring Task"></a>使用Spring Task</h3><p>在spring boot中我们可以直接使用注解的方式来实现定时任务，非常简单，需要的依赖</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework.boot<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-boot-starter<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"><span class="comment">&lt;!--通过注解的方式生成部分常用代码，减少重复代码编写--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.projectlombok<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>lombok<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">optional</span>&gt;</span>true<span class="tag">&lt;/<span class="name">optional</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework.boot<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-boot-starter-web<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework.boot<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-boot-starter-test<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">scope</span>&gt;</span>test<span class="tag">&lt;/<span class="name">scope</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p>创建任务类</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Slf</span>4j</span><br><span class="line"><span class="meta">@Component</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">TestSpringTask</span> </span>&#123;</span><br><span class="line">  <span class="comment">/**</span></span><br><span class="line"><span class="comment">   * 通过表达式来配置任务执行时间</span></span><br><span class="line"><span class="comment">   */</span></span><br><span class="line">  <span class="meta">@Scheduled</span>(cron = <span class="string">"0/5 * * * * *"</span>)</span><br><span class="line">  <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">scheduled</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    log.info(<span class="string">"=====&gt;&gt;&gt;&gt;&gt;使用cron:&#123;&#125;"</span>, System.currentTimeMillis());</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="comment">/**</span></span><br><span class="line"><span class="comment">   * 按一定频率执行的定时任务</span></span><br><span class="line"><span class="comment">   */</span></span><br><span class="line">  <span class="meta">@Scheduled</span>(fixedRate = <span class="number">5000</span>)</span><br><span class="line">  <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">scheduled1</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    log.info(<span class="string">"=====&gt;&gt;&gt;&gt;&gt;使用fixedRate:&#123;&#125;"</span>, System.currentTimeMillis());</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="comment">/**</span></span><br><span class="line"><span class="comment">   * 功能同fixedRate，但是可以配合initialDelay属性设置任务延时执行</span></span><br><span class="line"><span class="comment">   */</span></span><br><span class="line">  <span class="meta">@Scheduled</span>(fixedDelay = <span class="number">5000</span>)</span><br><span class="line">  <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">scheduled2</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    log.info(<span class="string">"=====&gt;&gt;&gt;&gt;&gt;使用fixedDelay:&#123;&#125;"</span>, System.currentTimeMillis());</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>在<code>Application</code>类上添加<code>@EnableScheduling</code>注解开启对定时任务的支持</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@SpringBootApplication</span></span><br><span class="line"><span class="comment">//开启对定时任务的支持(所有的定时任务都在同一线程上串行)</span></span><br><span class="line"><span class="meta">@EnableScheduling</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Application</span> </span>&#123;</span><br><span class="line">  <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">    SpringApplication.run(Application<span class="class">.<span class="keyword">class</span>)</span>;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>然后启动项目查看打印</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">2019-04-03 11:15:10.001  INFO 9252 --- [   scheduling-1] com.lx.timer.springtask.TestSpringTask   : &#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&gt;&gt;&gt;&gt;&gt;使用cron:1554261310001</span><br><span class="line">2019-04-03 11:15:12.209  INFO 9252 --- [   scheduling-1] com.lx.timer.springtask.TestSpringTask   : &#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&gt;&gt;&gt;&gt;&gt;使用fixedRate:1554261312209</span><br><span class="line">2019-04-03 11:15:12.211  INFO 9252 --- [   scheduling-1] com.lx.timer.springtask.TestSpringTask   : &#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&gt;&gt;&gt;&gt;&gt;使用fixedDelay:1554261312211</span><br><span class="line">2019-04-03 11:15:15.002  INFO 9252 --- [   scheduling-1] com.lx.timer.springtask.TestSpringTask   : &#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&gt;&gt;&gt;&gt;&gt;使用cron:1554261315002</span><br></pre></td></tr></table></figure>
<p>可以看到三个定时任务都已经执行，并且使同一个线程中串行执行，如果只有一个定时任务，这样做肯定没问题，当定时任务增多，如果一个任务卡死，会导致其他任务也无法执行，所以我们需要修改为多线程执行</p>
<h4 id="修改Spring-Task为多线程执行"><a href="#修改Spring-Task为多线程执行" class="headerlink" title="修改Spring Task为多线程执行"></a>修改Spring Task为多线程执行</h4><p>新建一个AsyncConfig类</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Configuration</span></span><br><span class="line"><span class="comment">//开启异步事件的支持</span></span><br><span class="line"><span class="meta">@EnableAsync</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">AsyncConfig</span> </span>&#123;</span><br><span class="line">  <span class="comment">/**</span></span><br><span class="line"><span class="comment">    * 以下参数均从yml配置文件中获取</span></span><br><span class="line"><span class="comment">    */</span></span><br><span class="line">  <span class="meta">@Value</span>(<span class="string">"$&#123;async.core-pool-size&#125;"</span>)</span><br><span class="line">  <span class="keyword">private</span> <span class="keyword">int</span> corePoolSize;</span><br><span class="line">  <span class="meta">@Value</span>(<span class="string">"$&#123;async.max-pool-size&#125;"</span>)</span><br><span class="line">  <span class="keyword">private</span> <span class="keyword">int</span> maxPoolSize;</span><br><span class="line">  <span class="meta">@Value</span>(<span class="string">"$&#123;async.queue-capacity&#125;"</span>)</span><br><span class="line">  <span class="keyword">private</span> <span class="keyword">int</span> quequeCapacity;</span><br><span class="line"></span><br><span class="line">  <span class="meta">@Bean</span></span><br><span class="line">  <span class="function"><span class="keyword">public</span> Executor <span class="title">taskExecutor</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    ThreadPoolTaskExecutor executor = <span class="keyword">new</span> ThreadPoolTaskExecutor();</span><br><span class="line">    executor.setCorePoolSize(corePoolSize);</span><br><span class="line">    executor.setMaxPoolSize(maxPoolSize);</span><br><span class="line">    executor.setQueueCapacity(quequeCapacity);</span><br><span class="line">    executor.initialize();</span><br><span class="line">    <span class="keyword">return</span> executor;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>然后再定时任务的类或者方法上添加<code>@Async</code></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Slf</span>4j</span><br><span class="line"><span class="meta">@Component</span></span><br><span class="line"><span class="comment">//对当前类开启异步（开启后每个方法的执行均在不同的线程中），也可以加在方法上</span></span><br><span class="line"><span class="meta">@Async</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">TestSpringTask</span> </span>&#123;</span><br><span class="line">  <span class="comment">/**</span></span><br><span class="line"><span class="comment">   * 通过表达式来配置任务执行时间</span></span><br><span class="line"><span class="comment">   */</span></span><br><span class="line">  <span class="meta">@Scheduled</span>(cron = <span class="string">"0/5 * * * * *"</span>)</span><br><span class="line">  <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">scheduled</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    log.info(<span class="string">"=====&gt;&gt;&gt;&gt;&gt;使用cron:&#123;&#125;"</span>, System.currentTimeMillis());</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="comment">/**</span></span><br><span class="line"><span class="comment">   * 按一定频率执行的定时任务</span></span><br><span class="line"><span class="comment">   */</span></span><br><span class="line">  <span class="meta">@Scheduled</span>(fixedRate = <span class="number">5000</span>)</span><br><span class="line">  <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">scheduled1</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    log.info(<span class="string">"=====&gt;&gt;&gt;&gt;&gt;使用fixedRate:&#123;&#125;"</span>, System.currentTimeMillis());</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="comment">/**</span></span><br><span class="line"><span class="comment">   * 功能同fixedRate，但是可以配合initialDelay属性设置任务延时执行</span></span><br><span class="line"><span class="comment">   */</span></span><br><span class="line">  <span class="meta">@Scheduled</span>(fixedDelay = <span class="number">5000</span>)</span><br><span class="line">  <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">scheduled2</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    log.info(<span class="string">"=====&gt;&gt;&gt;&gt;&gt;使用fixedDelay:&#123;&#125;"</span>, System.currentTimeMillis());</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>重启项目，可以看到每个任务都是在不同的线程</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">2019-04-03 11:19:47.020  INFO 13420 --- [ taskExecutor-4] com.lx.timer.springtask.TestSpringTask   : &#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&gt;&gt;&gt;&gt;&gt;使用fixedRate:1554261587020</span><br><span class="line">2019-04-03 11:19:47.024  INFO 13420 --- [ taskExecutor-5] com.lx.timer.springtask.TestSpringTask   : &#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&gt;&gt;&gt;&gt;&gt;使用fixedDelay:1554261587024</span><br><span class="line">2019-04-03 11:19:50.002  INFO 13420 --- [ taskExecutor-6] com.lx.timer.springtask.TestSpringTask   : &#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&gt;&gt;&gt;&gt;&gt;使用cron:1554261590002</span><br><span class="line">2019-04-03 11:19:52.020  INFO 13420 --- [ taskExecutor-7] com.lx.timer.springtask.TestSpringTask   : &#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&gt;&gt;&gt;&gt;&gt;使用fixedRate:1554261592020</span><br><span class="line">2019-04-03 11:19:52.025  INFO 13420 --- [ taskExecutor-8] com.lx.timer.springtask.TestSpringTask   : &#x3D;&#x3D;&#x3D;&#x3D;&#x3D;&gt;&gt;&gt;&gt;&gt;使用fixedDelay:1554261592025</span><br></pre></td></tr></table></figure>
<h4 id="执行时间的配置"><a href="#执行时间的配置" class="headerlink" title="执行时间的配置"></a>执行时间的配置</h4><p>在上面的定时任务中，我们在方法上使用<code>@Scheduled</code>注解来设置任务的执行时间，并且使用3三种属性配置方式</p>
<ul>
<li>fixedRate: 定义一个按照一定频率执行的定时任务</li>
<li>fixedDelay: 定义一个按一定频率执行的定时任务，与上边不同的是，该属性可以配合initialDelay，定义该任务延迟执行时间</li>
<li>cron: 通过表达式来配置任务执行时间</li>
</ul>
<h4 id="cron表达式详细参考下边的博客"><a href="#cron表达式详细参考下边的博客" class="headerlink" title="cron表达式详细参考下边的博客"></a>cron表达式详细参考下边的博客</h4><blockquote>
<p><a href="https://blog.csdn.net/qq_33430083/article/details/89024540" target="_blank" rel="noopener">https://blog.csdn.net/qq_33430083/article/details/89024540</a></p>
</blockquote>
<h3 id="整合Quartz"><a href="#整合Quartz" class="headerlink" title="整合Quartz"></a>整合Quartz</h3><p>添加依赖<br>如果SpringBoot版本是2.0.0以后的，则在spring-boot-starter中已经包含了quart的依赖，则可以直接使用spring-boot-starter-quartz依赖:</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework.boot<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-boot-starter-quartz<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p>如果是1.5.9则要使用以下添加依赖:</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.quartz-scheduler<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>quartz<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">version</span>&gt;</span>2.3.0<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-context-support<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p><strong>注意：如果项目中springboot使用的是springboot2.0以后的版本，去引用<code>quartz</code>和<code>spring-context-support</code>包的话，quartz任务并不会执行</strong></p>
<h4 id="1-创建任务类"><a href="#1-创建任务类" class="headerlink" title="1.创建任务类"></a>1.创建任务类</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">TestQuartz</span> <span class="keyword">extends</span> <span class="title">QuartzJobBean</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">  <span class="comment">/**</span></span><br><span class="line"><span class="comment">   * 执行定时任务</span></span><br><span class="line"><span class="comment">   * <span class="doctag">@param</span> jobExecutionContext</span></span><br><span class="line"><span class="comment">   * <span class="doctag">@throws</span> JobExecutionException</span></span><br><span class="line"><span class="comment">   */</span></span><br><span class="line">  <span class="meta">@Override</span></span><br><span class="line">  <span class="function"><span class="keyword">protected</span> <span class="keyword">void</span> <span class="title">executeInternal</span><span class="params">(JobExecutionContext jobExecutionContext)</span> <span class="keyword">throws</span> JobExecutionException </span>&#123;</span><br><span class="line">    System.out.println(<span class="string">"quartz task"</span> + System.currentTimeMillis());</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h4 id="2-创建配置类QuartzConfig"><a href="#2-创建配置类QuartzConfig" class="headerlink" title="2.创建配置类QuartzConfig"></a>2.创建配置类QuartzConfig</h4><p>执行方式有两种写法一种是使用<code>CronScheduleBuilder</code>类执行cron表达式<br>另一种是使用<code>SimpleScheduleBuilder</code>类指定间隔时间</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">QuartzConfig</span> </span>&#123;</span><br><span class="line">  <span class="meta">@Bean</span></span><br><span class="line">  <span class="function"><span class="keyword">public</span> JobDetail <span class="title">teatQuartzDetail</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> JobBuilder.newJob(TestQuartz<span class="class">.<span class="keyword">class</span>)</span></span><br><span class="line">            .withIdentity("testQuartz").storeDurably().build();</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="meta">@Bean</span></span><br><span class="line">  <span class="function"><span class="keyword">public</span> Trigger <span class="title">testQuartzTrigger</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    <span class="comment">//使用cron表达式</span></span><br><span class="line">    CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule(<span class="string">"*/5 * * * * ?"</span>);</span><br><span class="line">    <span class="comment">//指定间隔时间</span></span><br><span class="line"><span class="comment">//    SimpleScheduleBuilder scheduleBuilder = SimpleScheduleBuilder.simpleSchedule()</span></span><br><span class="line"><span class="comment">//            //设置时间周期单位秒，也可选择其他函数</span></span><br><span class="line"><span class="comment">//            .withIntervalInSeconds(3)</span></span><br><span class="line"><span class="comment">//            .repeatForever();</span></span><br><span class="line">    <span class="keyword">return</span> TriggerBuilder.newTrigger().forJob(teatQuartzDetail())</span><br><span class="line">            .withIdentity(<span class="string">"testQuartz"</span>)</span><br><span class="line">            .withSchedule(cronScheduleBuilder)</span><br><span class="line">            .build();</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h4 id="3-启动项目"><a href="#3-启动项目" class="headerlink" title="3.启动项目"></a>3.启动项目</h4><p>控制台打印如下，配置成功</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">quartz task1554273605007</span><br><span class="line">quartz task1554273610000</span><br><span class="line">quartz task1554273615000</span><br></pre></td></tr></table></figure>

<h2 id="Spring-Task和Quarz的区别"><a href="#Spring-Task和Quarz的区别" class="headerlink" title="Spring Task和Quarz的区别"></a>Spring Task和Quarz的区别</h2><p>Quartz在项目启动的时候会执行，使用注解的方式会在项目启动完成后时间算起执行，少一次执行调度</p>
<h2 id="Demo地址"><a href="#Demo地址" class="headerlink" title="Demo地址"></a>Demo地址</h2><blockquote>
<p><a href="https://gitee.com/fengzxia/spring-boot-timer-learn" target="_blank" rel="noopener">https://gitee.com/fengzxia/spring-boot-timer-learn</a></p>
</blockquote>
<h2 id="参考博客地址"><a href="#参考博客地址" class="headerlink" title="参考博客地址"></a>参考博客地址</h2><blockquote>
<p><a href="https://juejin.im/post/5ca24fb1e51d454a490a4809?utm_source=gold_browser_extension" target="_blank" rel="noopener">https://juejin.im/post/5ca24fb1e51d454a490a4809?utm_source=gold_browser_extension</a></p>
</blockquote>

<div id="gitalk-container"></div>
<script src="https://cdn.bootcss.com/blueimp-md5/2.12.0/js/md5.min.js"></script><link rel="stylesheet" href="https://unpkg.com/gitalk/dist/gitalk.css"><script src="https://unpkg.com/gitalk/dist/gitalk.min.js"></script>

		<script>
		var gitalkConfig = {"clientID":"28a065e3b5afee156f4f","clientSecret":"e18104d6f3219526491fc42c892ff6a26b8f3e74","repo":"blog_comment","owner":"FENG1000","admin":["FENG1000"],"distractionFreeMode":false};
	    gitalkConfig.id = md5(location.pathname);
		var gitalk = new Gitalk(gitalkConfig);
	    gitalk.render("gitalk-container");
	    </script>
    
  <blockquote>
      <p>
          作者: 只是学习学习<br>
          邮箱: fengzxia1000@163.com<br>
          原文地址: <a href="https://fengzxia.gitee.io/posts/493442a1.html" target="_blank" rel="noopener">https://fengzxia.gitee.io/posts/493442a1.html</a><br>
          版权声明: 商业转载请联系作者获得授权，非商业转载请注明出处。
      </p>
  </blockquote>

  <script>
      function setClipboardText(event){
          // clipboardData 对象是为通过编辑菜单、快捷菜单和快捷键执行的编辑操作所保留的，也就是你复制或者剪切内容
          let clipboardData = event.clipboardData || window.clipboardData;
          // 如果未复制或者未剪切，则return出去
          if (!clipboardData) { return; }
          event.preventDefault();
          // Selection 对象，表示用户选择的文本范围或光标的当前位置。
          //     声明一个变量接收 -- 用户输入的剪切或者复制的文本转化为字符串
          let text = window.getSelection().toString();

          if (text) {
            // 如果文本存在则先取消文本默认事件
            event.preventDefault();
            // 通过调用常clipboardData对象的 setData(format, data) 方法；来设置相关文本
            // format: 一个DOMString 表示要添加到 drag object的拖动数据的类型。
            // data: 一个 DOMString表示要添加到 drag object的数据。
            var copyright = '\n\n'
                            + '\n著作权归作者所有。'
                            + '\n商业转载请联系作者获得授权，非商业转载请注明出处。'
                            + '\n作者: 只是学习学习'
                            + '\n邮箱: fengzxia1000@163.com'
                            + '\n原文地址: https://fengzxia.gitee.io/posts/493442a1.html'

            clipboardData.setData('text/plain', text + copyright);

          }
      };
      var contents = document.getElementsByClassName("content");
      // 监听文章内容的copy事件
      contents[0].addEventListener('copy',function(e){
          setClipboardText(e);
      });
  </script>

  </div>
</article>




        
          <div id="footer-post-container">
  <div id="footer-post">

    <div id="nav-footer" style="display: none">
      <ul>
         
          <li><a href="/">主页</a></li>
         
          <li><a href="/archives/">归档</a></li>
         
          <li><a href="/categories/">分类</a></li>
         
          <li><a href="/tags/">标签</a></li>
         
          <li><a href="/search/">搜索</a></li>
         
          <li><a href="/about/">关于</a></li>
         
          <li><a href="/friend_ship/">友链</a></li>
        
      </ul>
    </div>

    <div id="toc-footer" style="display: none">
      <ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#实现定时任务的几种方式"><span class="toc-number">1.</span> <span class="toc-text">实现定时任务的几种方式</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#使用Timer"><span class="toc-number">1.1.</span> <span class="toc-text">使用Timer</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#使用ScheduledExecutorService"><span class="toc-number">1.2.</span> <span class="toc-text">使用ScheduledExecutorService</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#使用Spring-Task"><span class="toc-number">1.3.</span> <span class="toc-text">使用Spring Task</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#修改Spring-Task为多线程执行"><span class="toc-number">1.3.1.</span> <span class="toc-text">修改Spring Task为多线程执行</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#执行时间的配置"><span class="toc-number">1.3.2.</span> <span class="toc-text">执行时间的配置</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#cron表达式详细参考下边的博客"><span class="toc-number">1.3.3.</span> <span class="toc-text">cron表达式详细参考下边的博客</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#整合Quartz"><span class="toc-number">1.4.</span> <span class="toc-text">整合Quartz</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#1-创建任务类"><span class="toc-number">1.4.1.</span> <span class="toc-text">1.创建任务类</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#2-创建配置类QuartzConfig"><span class="toc-number">1.4.2.</span> <span class="toc-text">2.创建配置类QuartzConfig</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#3-启动项目"><span class="toc-number">1.4.3.</span> <span class="toc-text">3.启动项目</span></a></li></ol></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Spring-Task和Quarz的区别"><span class="toc-number">2.</span> <span class="toc-text">Spring Task和Quarz的区别</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Demo地址"><span class="toc-number">3.</span> <span class="toc-text">Demo地址</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#参考博客地址"><span class="toc-number">4.</span> <span class="toc-text">参考博客地址</span></a></li></ol>
    </div>

    <div id="share-footer" style="display: none">
      <ul>
  <li><a class="icon" href="http://www.facebook.com/sharer.php?u=https://fengzxia.gitee.io/posts/493442a1.html" target="_blank" rel="noopener"><i class="fab fa-facebook fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://twitter.com/share?url=https://fengzxia.gitee.io/posts/493442a1.html&text=Spring Boot实现定时任务的4种方式" target="_blank" rel="noopener"><i class="fab fa-twitter fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://www.linkedin.com/shareArticle?url=https://fengzxia.gitee.io/posts/493442a1.html&title=Spring Boot实现定时任务的4种方式" target="_blank" rel="noopener"><i class="fab fa-linkedin fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://pinterest.com/pin/create/bookmarklet/?url=https://fengzxia.gitee.io/posts/493442a1.html&is_video=false&description=Spring Boot实现定时任务的4种方式" target="_blank" rel="noopener"><i class="fab fa-pinterest fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="mailto:?subject=Spring Boot实现定时任务的4种方式&body=Check out this article: https://fengzxia.gitee.io/posts/493442a1.html"><i class="fas fa-envelope fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://getpocket.com/save?url=https://fengzxia.gitee.io/posts/493442a1.html&title=Spring Boot实现定时任务的4种方式" target="_blank" rel="noopener"><i class="fab fa-get-pocket fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://reddit.com/submit?url=https://fengzxia.gitee.io/posts/493442a1.html&title=Spring Boot实现定时任务的4种方式" target="_blank" rel="noopener"><i class="fab fa-reddit fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://www.stumbleupon.com/submit?url=https://fengzxia.gitee.io/posts/493442a1.html&title=Spring Boot实现定时任务的4种方式" target="_blank" rel="noopener"><i class="fab fa-stumbleupon fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://digg.com/submit?url=https://fengzxia.gitee.io/posts/493442a1.html&title=Spring Boot实现定时任务的4种方式" target="_blank" rel="noopener"><i class="fab fa-digg fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://www.tumblr.com/share/link?url=https://fengzxia.gitee.io/posts/493442a1.html&name=Spring Boot实现定时任务的4种方式&description=" target="_blank" rel="noopener"><i class="fab fa-tumblr fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://news.ycombinator.com/submitlink?u=https://fengzxia.gitee.io/posts/493442a1.html&t=Spring Boot实现定时任务的4种方式" target="_blank" rel="noopener"><i class="fab fa-hacker-news fa-lg" aria-hidden="true"></i></a></li>
</ul>

    </div>

    <div id="actions-footer">
        <a id="menu" class="icon" href="#" onclick="$('#nav-footer').toggle();return false;"><i class="fas fa-bars fa-lg" aria-hidden="true"></i> 菜单</a>
        <a id="toc" class="icon" href="#" onclick="$('#toc-footer').toggle();return false;"><i class="fas fa-list fa-lg" aria-hidden="true"></i> 目录</a>
        <a id="share" class="icon" href="#" onclick="$('#share-footer').toggle();return false;"><i class="fas fa-share-alt fa-lg" aria-hidden="true"></i> 分享</a>
        <a id="top" style="display:none" class="icon" href="#" onclick="$('html, body').animate({ scrollTop: 0 }, 'fast');"><i class="fas fa-chevron-up fa-lg" aria-hidden="true"></i> 返回顶部</a>
    </div>

  </div>
</div>

        
        <footer id="footer">
  <div class="footer-left">
    Copyright &copy;
    
    
    2019-2020
    只是学习学习
  </div>
  <div class="footer-right">
    <nav>
      <ul>
         
          <li><a href="/">主页</a></li>
         
          <li><a href="/archives/">归档</a></li>
         
          <li><a href="/categories/">分类</a></li>
         
          <li><a href="/tags/">标签</a></li>
         
          <li><a href="/search/">搜索</a></li>
         
          <li><a href="/about/">关于</a></li>
         
          <li><a href="/friend_ship/">友链</a></li>
        
      </ul>
    </nav>
  </div>
</footer>

    </div>
    <!-- styles -->

<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">


<link rel="stylesheet" href="/lib/justified-gallery/css/justifiedGallery.min.css">


    <!-- jquery -->

<script src="/lib/jquery/jquery.min.js"></script>


<script src="/lib/justified-gallery/js/jquery.justifiedGallery.min.js"></script>

<!-- clipboard -->

  
<script src="/lib/clipboard/clipboard.min.js"></script>

  <script type="text/javascript">
  $(function() {
    // copy-btn HTML
    var btn = "<span class=\"btn-copy tooltipped tooltipped-sw\" aria-label=\"复制到粘贴板!\">";
    btn += '<i class="far fa-clone"></i>';
    btn += '</span>'; 
    // mount it!
    $(".highlight table").before(btn);
    var clip = new ClipboardJS('.btn-copy', {
      text: function(trigger) {
        return Array.from(trigger.nextElementSibling.querySelectorAll('.code')).reduce((str,it)=>str+it.innerText+'\n','')
      }
    });
    clip.on('success', function(e) {
      e.trigger.setAttribute('aria-label', "复制成功!");
      e.clearSelection();
    })
  })
  </script>


<script src="/js/main.js"></script>

<!-- search -->

<!-- Google Analytics -->

<!-- Baidu Analytics -->

    <script type="text/javascript">
        var _hmt = _hmt || [];
        (function() {
            var hm = document.createElement("script");
            hm.src = "https://hm.baidu.com/hm.js?54382acbd8c4bc66535bce135e0ac166";
            var s = document.getElementsByTagName("script")[0];
            s.parentNode.insertBefore(hm, s);
        })();
    </script>

<!-- Disqus Comments -->


</body>
<script>
(function(){
    var bp = document.createElement('script');
    var curProtocol = window.location.protocol.split(':')[0];
    if (curProtocol === 'https'){
   bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';
  }
  else{
  bp.src = 'http://push.zhanzhang.baidu.com/push.js';
  }
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(bp, s);
})();
</script>
</html>
